package com.desktopmng.core;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import oracle.jdbc.driver.OracleTypes;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.RowMapperResultSetExtractor;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;

/**
 * 执行存储过程，返回结果集
 * @author Administrator
 *
 */
public class DesktopStoredProcedure extends StoredProcedure implements Serializable {

	private Map map = new HashMap();
	
	private List list = new ArrayList();

	// 行映射
	private RowMapper rm = new RowMapper() {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			DataBean obj = new DataBean();
			if (null != rs) {
				obj.setId(rs.getString(1));
				obj.setName(rs.getString(2));
			}
			return obj;
		}
	};

	// 回调方法
	private RowMapperResultSetExtractor callback = new RowMapperResultSetExtractor(rm) {
		public Object extractData(ResultSet rs) throws SQLException {// 回调处理
			/*int count = rs.getMetaData().getColumnCount();
			String[] header = new String[count];
			for (int i = 0; i < count; i++) {
				header[i] = rs.getMetaData().getColumnName(i + 1);
			}
			while (rs.next()) {
				HashMap row = new HashMap(count + 7);
				for (int i = 0; i < count; i++) {
					row.put(header[i], rs.getString(i + 1));
				}
				list.add(row);
			}*/	
			while (rs.next()) {
				DataBean obj = new DataBean();
				obj.setId(rs.getString(1));
				obj.setName(rs.getString(2));
				list.add(obj);
			}
			return null;
		}
	};
	
	public List getCursorResult() {
		return list;
	}

	public DesktopStoredProcedure() {
		super();
	}

	public DesktopStoredProcedure(DataSource ds) {
		this.setDataSource(ds);
		this.setIntegerOutParam("ret");
		this.setVarcharOutParam("errm");
		this.declareParameter(new SqlOutParameter("data", OracleTypes.CURSOR,
				rm));
	}

	public void setValue(Object key, Object obj) {
		map.put(key, obj);
	}

	public Map execute() {
		if (this.getSql() == null || this.getSql().equals(""))
			return null;
		this.compile();
		return execute(map);
	}

	public void setVarcharParam(String param) {
		this.declareParameter(new SqlParameter(param, Types.VARCHAR));
	}

	public void setDoubleParam(String param) {
		this.declareParameter(new SqlParameter(param, Types.DOUBLE));
	}

	public void setIntegerParam(String param) {
		this.declareParameter(new SqlParameter(param, Types.INTEGER));
	}

	public void setVarcharOutParam(String param) {
		this.declareParameter(new SqlOutParameter(param, Types.VARCHAR));
	}

	public void setDoubleOutParam(String param) {
		this.declareParameter(new SqlOutParameter(param, Types.DOUBLE));
	}

	public void setIntegerOutParam(String param) {
		this.declareParameter(new SqlOutParameter(param, Types.INTEGER));
	}

}
